﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace WebApplication1.Filter
{
    public class ApiContentFilterAttribute : Attribute, IResultFilter, IFilterMetadata
    {
        //返回结果后，不可修改result
        public void OnResultExecuted(ResultExecutedContext context)
        {
            //请求数据
            var request = context.HttpContext.Request;
            request.Body.Position = 0;
            using var requestReader = new StreamReader(request.Body);
            string requestContent = requestReader.ReadToEnd();
            request.Body.Position = 0;

            //响应数据
            context.HttpContext.Response.Body.Position = 0;
            StreamReader responsestream = new StreamReader(context.HttpContext.Response.Body);
            string responsebody = responsestream.ReadToEnd();
            context.HttpContext.Response.Body.Position = 0;         
        }

        //返回结果前，可修改result
        public void OnResultExecuting(ResultExecutingContext context)
        {
            
        }
    }
}
